*==========================================================
* Table4
*==========================================================
**model（4）
cd F:\PLOS\Data&code
use basicdata.dta, clear
sort code year
tsset code year
global y       "LPeerRD1"
global control "LCash LLev LRoa LTobinQ  LTangible LAge LSize LPeerCash LPeerLev LPeerRoa LPeerTobinQ LPeerTangible LPeerAge LPeerSize"
reghdfe RD1   $y $control,absorb( code year) 
est store m1
global x       "LPeerRD2"
global control "LCash LLev LRoa LTobinQ  LTangible LAge LSize LPeerCash LPeerLev LPeerRoa LPeerTobinQ LPeerTangible LPeerAge LPeerSize"
reghdfe RD2      $x $control,absorb( code year)  
est store m2

**model（5）
global y       "LPeerRD1 LEPUmeanLPeerRD1   LEPUmean lnM2  eei"
global control "LCash LLev LRoa LTobinQ  LTangible  LAge LSize LPeerCash LPeerLev LPeerRoa LPeerTobinQ  LPeerTangible  LPeerAge LPeerSize"
reghdfe RD1      $y  $control ,absorb(code)  
est store m3

global x       "LPeerRD2 LEPUmeanLPeerRD2  LEPUmean lnM2  eei"
global control "LCash LLev LRoa LTobinQ  LTangible  LAge LSize LPeerCash LPeerLev LPeerRoa LPeerTobinQ  LPeerTangible  LPeerAge LPeerSize "
reghdfe RD2      $x  $control ,absorb(code) 
est store m4

local mm "m1 m2 m3 m4"
esttab `mm' using Table4.rtf, mtitle(`mm') order ($y $x) s(N r2, fmt(%12.0f %9.3f)) compress nogap ///
	                star(* 0.1 ** 0.05 *** 0.01) 	b(%12.4f) t(%12.3f)		

*=======================================================================================
* Table5  
*=======================================================================================
**Endogeneity test of Model(4)  
cd F:\PLOS\Data&code
use basicdata.dta, clear
tabulate year, gen(REG)  

global control "LCash LLev LRoa LTobinQ  LTangible LAge LSize LPeerCash LPeerLev LPeerRoa LPeerTobinQ LPeerTangible LPeerAge LPeerSize"
xtivreg2 RD1 $control (LPeerRD1 = LLPeerRD1)  REG*,fe gmm first
est store m1

xtivreg2 RD2 $control (LPeerRD2 = LLPeerRD2)  REG*,fe gmm first
est store m2
local mm "m1 m2"
esttab `mm' using Table5_Model4.rtf, mtitle(`mm')  s(N r2, fmt(%12.0f %9.3f)) compress nogap ///
	                star(* 0.1 ** 0.05 *** 0.01) drop (REG*)	b(%12.4f) t(%12.3f)		
**Endogeneity test of Model(5) 
cd F:\PLOS\Data&code
use basicdata.dta, clear
sort code year
tabulate year, gen(REG)    
gen LEPUmeanL股票年特质收益率_mean=LEPUmean*L股票年特质收益率_mean 
gen LEPUmeanLLPeerRD2=LEPUmean*LLPeerRD2
gen LEPUmeanLLPeerRD1=LEPUmean*LLPeerRD1
gen LGEPUmeanLLPeerRD1=LGEPUmean*LLPeerRD1
gen LGEPUmeanLLPeerRD2=LGEPUmean*LLPeerRD2

global control " LCash LLev LRoa LTobinQ  LTangible  LAge LSize LPeerCash LPeerLev LPeerRoa LPeerTobinQ  LPeerTangible  LPeerAge LPeerSize lnM2  eei"
xtivreg2 RD1 $control (LPeerRD1 LEPUmeanLPeerRD1 LEPUmean=LLPeerRD1 LGEPUmeanLLPeerRD1 LGEPUmean),fe gmm  first
est store m1
xtivreg2 RD2 $control (LPeerRD2 LEPUmeanLPeerRD2 LEPUmean=LLPeerRD2 LGEPUmeanLLPeerRD2 LGEPUmean),fe gmm  first
est store m2
local mm "m1 m2"
       esttab `mm' using Table5_Model5.rtf, mtitle(`mm')  s(N r2, fmt(%12.0f %9.3f)) compress nogap ///
	                star(* 0.1 ** 0.05 *** 0.01)	b(%12.4f) t(%12.3f)		
*==================================================================
*Table6 
*==================================================================
*Table6 Tobit Model
cd F:\PLOS\Data&code
use basicdata.dta, clear
sort code year
tsset code year
global y       "LPeerRD1"
global control "LCash LLev LRoa LTobinQ  LTangible LAge LSize LPeerCash LPeerLev LPeerRoa LPeerTobinQ LPeerTangible LPeerAge LPeerSize"
xttobit RD1  $y $control i.year,ll(0) nolog tobit
est store m1

global y       "LPeerRD1 LEPUmean LEPUmeanLPeerRD1 lnM2  eei"
global control "LCash LLev LRoa LTobinQ  LTangible  LAge LSize LPeerCash LPeerLev LPeerRoa LPeerTobinQ  LPeerTangible  LPeerAge LPeerSize "
xttobit RD1  $y $control ,ll(0) nolog tobit  //可以
est store m2

local mm "m1 m2"
       esttab `mm' using Table6_Tobit.rtf, mtitle(`mm')  s(N r2, fmt(%12.0f %9.3f)) compress nogap ///
	                star(* 0.1 ** 0.05 *** 0.01) drop (20*)	b(%12.4f) t(%12.3f)		

**Table6 supplementary missing values of R&D input data  
** model(4)
cd F:\PLOS\Data&code
use basicdata_supplementary.dta, clear
global y       "LPeerRD1"
global control "LCash LLev LRoa LTobinQ  LTangible LAge LSize LPeerCash LPeerLev LPeerRoa LPeerTobinQ LPeerTangible LPeerAge LPeerSize"
reghdfe RD1   $y $control,absorb( code year)  
est store m1
** model(5)
global y       "LPeerRD1 LEPUmeanLPeerRD1   LEPUmean lnM2  eei"
global control "LCash LLev LRoa LTobinQ  LTangible  LAge LSize LPeerCash LPeerLev LPeerRoa LPeerTobinQ  LPeerTangible  LPeerAge LPeerSize"
reghdfe RD1      $y  $control ,absorb(code)  
est store m2

local mm "m1 m2"
esttab `mm' using Table6_supplementary.rtf, mtitle(`mm') order ($y ) s(N r2, fmt(%12.0f %9.3f)) compress nogap ///
	                star(* 0.1 ** 0.05 *** 0.01) 	b(%12.4f) t(%12.3f)	

*============================================================================================
* Table7 Change the measurement method of R&D input of enterprises in the same group       
*============================================================================================
cd F:\PLOS\Data&code
use basicdata.dta, clear
** model(4)
sort code year
tsset code year
global y       "LPeerRD1_median"
global control "LCash LLev LRoa LTobinQ LTangible LSize LAge  LPeerCash_median LPeerLev_median LPeerRoa_median LPeerTobinQ_median LPeerTangible_median LPeerAge_median LPeerSize_median"
reghdfe RD1      $y $control,absorb( code year)  
est store m1
** model(5)
gen LEPUmeanLPeerRD1_median=LEPUmean*LPeerRD1_median
gen LEPUmeanLPeerRD2_median=LEPUmean*LPeerRD2_median

sort code year
tsset code year
global y       "LPeerRD1_median LEPUmean LEPUmeanLPeerRD1_median lnM2  eei"
global control "LCash LLev LRoa LTobinQ LTangible LSize LAge  LPeerCash_median LPeerLev_median LPeerRoa_median LPeerTobinQ_median LPeerTangible_median LPeerAge_median LPeerSize_median"
reghdfe RD1      $y  $control  ,absorb(code)  //好
est store m2
*===================================================================================================================
*  Table7 Change the measurement method of economic policy uncertainty     
*===================================================================================================================
cd F:\PLOS\Data&code
use basicdata.dta, clear
** model(4)
global y       "LEPUmedianLPeerRD1 LPeerRD1 LEPUmedian  lnM2  eei"
global control "LCash LLev LRoa LTobinQ  LTangible  LAge LSize LPeerCash LPeerLev LPeerRoa LPeerTobinQ  LPeerTangible  LPeerAge LPeerSize"
reghdfe RD1      $y  $control ,absorb(code)  //好
est store m3
** model(5)
global y       "LEPUmean1LPeerRD1  LPeerRD1 LEPUmean1 lnM2  eei"
global control "LCash LLev LRoa LTobinQ  LTangible  LAge LSize LPeerCash LPeerLev LPeerRoa LPeerTobinQ  LPeerTangible  LPeerAge LPeerSize"
reghdfe RD1      $y  $control ,absorb(code)  //好
est store m4
local mm "m1 m2 m3 m4"
esttab `mm' using Table7.rtf, mtitle(`mm') order ($y) s(N r2, fmt(%12.0f %9.3f)) compress nogap ///
	                star(* 0.1 ** 0.05 *** 0.01) 	b(%12.4f) t(%12.3f)		
*===================================================================================================================
*  Table8 Ceos age     
*===================================================================================================================
*  Table8 Ceos age  
cd F:\PLOS\Data&code
use basicdata.dta, clear
merge 1:1 code year  using CEO_age.dta
drop if _merge==1
drop if _merge==2
drop _merge
drop if missing(lnCeoage)  

//CEO年龄为行业内最小的 50%时，DummyCEOchar取值为1，否则为0
bysort Ind: egen lnCeoage_p50= pctile(lnCeoage), p(50)
keep if lnCeoage>lnCeoage_p50
sort code year
tsset code year

global y       "LPeerRD1 LEPUmeanLPeerRD1   LEPUmean lnM2  eei"
global control "LCash LLev LRoa LTobinQ  LTangible  LAge LSize LPeerCash LPeerLev LPeerRoa LPeerTobinQ  LPeerTangible  LPeerAge LPeerSize"
reghdfe RD1      $y  $control ,absorb(code)  
est store m1

cd F:\PLOS\Data&code
use basicdata.dta, clear
merge 1:1 code year  using CEO_age.dta
drop if _merge==1
drop if _merge==2
drop _merge
drop if missing(lnCeoage)  

bysort Ind: egen lnCeoage_p50= pctile(lnCeoage), p(50)
keep if lnCeoage<=lnCeoage_p50
sort code year
tsset code year

global y       "LPeerRD1 LEPUmeanLPeerRD1   LEPUmean lnM2  eei"
global control "LCash LLev LRoa LTobinQ  LTangible  LAge LSize LPeerCash LPeerLev LPeerRoa LPeerTobinQ  LPeerTangible  LPeerAge LPeerSize"
reghdfe RD1      $y  $control ,absorb(code)  
est store m2

*  Table8 CEO tenure  
cd F:\PLOS\Data&code
use basicdata.dta, clear
merge 1:1 code year  using CEO_tenure.dta
drop if _merge==2
drop _merge
bysort Ind: egen tenure_p50= pctile(tenure), p(50)
keep if tenure>tenure_p50
sort code year
tsset code year

global y       "LPeerRD1 LEPUmeanLPeerRD1   LEPUmean lnM2  eei"
global control "LCash LLev LRoa LTobinQ  LTangible  LAge LSize LPeerCash LPeerLev LPeerRoa LPeerTobinQ  LPeerTangible  LPeerAge LPeerSize"
reghdfe RD1      $y  $control ,absorb(code)  //好
est store m3

cd F:\PLOS\Data&code
use basicdata.dta, clear
merge 1:1 code year  using CEO_tenure.dta
drop if _merge==2
drop _merge
bysort Ind: egen tenure_p50= pctile(tenure), p(50)
keep if tenure<=tenure_p50
sort code year
tsset code year
global y       "LPeerRD1 LEPUmeanLPeerRD1   LEPUmean lnM2  eei"
global control "LCash LLev LRoa LTobinQ  LTangible  LAge LSize LPeerCash LPeerLev LPeerRoa LPeerTobinQ  LPeerTangible  LPeerAge LPeerSize"
reghdfe RD1      $y  $control ,absorb(code)  //好
est store m4

*  Table8 executive pay   
cd F:\PLOS\Data&code
use basicdata.dta, clear
merge 1:1 code year  using executive_pay.dta
drop if _merge==1
drop if _merge==2
drop _merge
bysort Ind: egen pay_p50= pctile(pay), p(50)
keep if pay<=pay_p50
sort code year
tsset code year

global y       "LPeerRD1 LEPUmeanLPeerRD1   LEPUmean lnM2  eei"
global control "LCash LLev LRoa LTobinQ  LTangible  LAge LSize LPeerCash LPeerLev LPeerRoa LPeerTobinQ  LPeerTangible  LPeerAge LPeerSize"
reghdfe RD1      $y  $control ,absorb(code)  //好
est store m5

cd F:\PLOS\Data&code
use basicdata.dta, clear
merge 1:1 code year  using executive_pay.dta
drop if _merge==1
drop if _merge==2
drop _merge
bysort Ind: egen pay_p50= pctile(pay), p(50)
keep if pay>pay_p50
sort code year
tsset code year
global y       "LPeerRD1 LEPUmeanLPeerRD1   LEPUmean lnM2  eei"
global control "LCash LLev LRoa LTobinQ  LTangible  LAge LSize LPeerCash LPeerLev LPeerRoa LPeerTobinQ  LPeerTangible  LPeerAge LPeerSize"
reghdfe RD1      $y  $control ,absorb(code)  
est store m6

local mm "m1 m2 m3 m4 m5 m6"
esttab `mm' using Table8.rtf, mtitle(`mm') order ($y $x) s(N r2, fmt(%12.0f %9.3f)) compress nogap ///
	                star(* 0.1 ** 0.05 *** 0.01) 	b(%12.4f) t(%12.3f)		
*===================================================================================================================
* Table9 mediating effect     
*===================================================================================================================
cd F:\PLOS\Data&code
use basicdata.dta, clear
sort code year
gen LSALPeerRD1=LSA*LPeerRD1
global x       "LPeerRD1 LEPUmean LEPUmeanLPeerRD1 lnM2  eei"
global control "LCash LLev LRoa LTobinQ  LTangible  LAge LSize LPeerCash LPeerLev LPeerRoa LPeerTobinQ  LPeerTangible  LPeerAge LPeerSize "
reghdfe RD1      $x  $control ,absorb(code)  
est store m1
global x       "LEPUmean lnM2  eei"
global control "LCash LLev LRoa LTobinQ  LTangible  LAge LSize LPeerCash LPeerLev LPeerRoa LPeerTobinQ  LPeerTangible  LPeerAge LPeerSize "
reghdfe LSA      $x  $control ,absorb(code)  
est store m2

global y       " LPeerRD1 LEPUmean LEPUmeanLPeerRD1 LSA LSALPeerRD1 lnM2  eei" 
global control "LCash LLev LRoa LTobinQ  LTangible  LAge LSize LPeerCash LPeerLev LPeerRoa LPeerTobinQ  LPeerTangible  LPeerAge LPeerSize "
reghdfe RD1      $y  $control  ,absorb(code)  
est store m3

local mm "m1 m2 m3"
esttab `mm' using Table9.rtf, mtitle(`mm') order ($y $x) s(N r2, fmt(%12.0f %9.3f)) compress nogap ///
	                star(* 0.1 ** 0.05 *** 0.01) 	b(%12.4f) t(%12.3f)		
					

